#include "AEEAppGen.h"        // Applet helper file
#include "helloworld.bid"		// Applet-specific header that contains class ID
#include "nmdef.h"

#include "resource.brh"
#include "AEEStdLib.h"
#include "AEEGraphics.h"


#include "AEEPoint.h"

#include "AEEImage.h"

#include "AEEGraphics.h"

#include "AEEMenu.h"

//resource.brh
typedef struct _MainApp
{
	AEEApplet a ;

	uint16		m_cxScreen, m_cyScreen;

	IMenuCtl*	m_pIMainMenu;		// Pointer to Main menu control
	IImage*		m_pImage;			// 



}MainApp;


static boolean HandleEvent(AEEApplet * pme, AEEEvent eCode,uint16 wParam, uint32 dwParam);
static void ShowMainMenu(MainApp* pMe);
boolean InitAppData(MainApp* pApp);

static boolean OnStart(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
static boolean OnStop(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
static boolean OnCommand(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
static boolean OnKey(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);

static void OnLine(MainApp* pApp);
static void OnPoint(MainApp* pApp);
static void Oncircle(MainApp* pApp);
static void OnRectangle(MainApp* pApp);
static void OnText(MainApp* pApp);
static void OnPicture(MainApp* pApp);
static void OnSetting(MainApp* pApp);
static void OnDownload(MainApp* pApp);

static void HideImg(MainApp* pApp);

//result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_LINE, IDS_DRAW_LINE, NULL, 0);
//result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_POINT, IDS_DRAW_POINT, NULL, 1);
//result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_CIRCLE, IDS_DRAW_CIRCLE, NULL, 2);
//result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_RECTANGLE, IDS_DRAW_RECTANGLE, NULL, 3);
//result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_TEXT, IDS_DRAW_TEXT, NULL, 4);
//result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_PICTURE, IDS_DRAW_PICTURE, NULL, 5);
//result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_SETTING, IDS_SETTING, NULL, 6);



static boolean HandleEvent(AEEApplet * pme, AEEEvent eCode,uint16 wParam, uint32 dwParam);


int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * pMod,void ** ppObj)
{

	*ppObj = NULL;

	if(AEEApplet_New( sizeof(MainApp),                  // Size of our private class
		ClsId,                              // Our class ID
		pIShell,                            // Shell interface
		pMod,                               // Module instance
		(IApplet**)ppObj,                   // Return object
		(AEEHANDLER)HandleEvent, // Our event handler
		NULL))  
		
	{
		if(InitAppData((MainApp*)*ppObj))
		{
			//Data initialized successfully
			return(AEE_SUCCESS);
		}
		else
		{
			IAPPLET_Release((IApplet*)*ppObj);
			return EFAILED;
		}

	}

	return (EFAILED);
}


static boolean HandleEvent(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{  

	MainApp * pMainApp = (MainApp *)pMe;

	//if( pMainApp->m_pIMainMenu && IMENUCTL_HandleEvent( pMainApp->m_pIMainMenu, eCode, wParam, dwParam) )
	//	return TRUE;


	switch (eCode)
	{
	case EVT_APP_START:  
		{
			DBGPRINTF("EVT_APP_START");
			if (OnStart(pMe, eCode, wParam, dwParam))
			{
				return TRUE;
			}
		}

	case EVT_APP_STOP:
		DBGPRINTF("EVT_APP_STOP");

		if (OnStop(pMe, eCode, wParam, dwParam))
		{
			return TRUE;
		}
		break;
	case EVT_COMMAND:
		DBGPRINTF("EVT_COMMAND");
		{
			if (OnCommand(pMe, eCode, wParam, dwParam))
			{
				return TRUE;
			}
		}
		break;
	case EVT_KEY:
		DBGPRINTF("EVT_KEY");
		{
			if (OnKey(pMe, eCode, wParam, dwParam))
			{
				return TRUE;
			}
		}
		break;

	default:
		DBGPRINTF("HandleEvent default");
		break;
	}
	return(FALSE);
}


boolean InitAppData(MainApp* pApp)
{
	MainApp* pMainApp = (MainApp*)pApp;

	AEEDeviceInfo di;


	// Initialize Data Members
	MEMSET(((char*)pMainApp) + sizeof(AEEApplet), 0, sizeof(*pMainApp) - sizeof(AEEApplet));
	pMainApp->m_pIMainMenu = NULL;
	pMainApp->m_pImage = NULL;

	ISHELL_GetDeviceInfo(pApp->a.m_pIShell,&di);
	pApp->m_cxScreen = di.cxScreen;
	pApp->m_cyScreen = di.cyScreen;



	return TRUE;
}


static void ShowMainMenu(MainApp* pMe)
{
	int result = -1;

	if (!pMe->m_pIMainMenu)
	{
		if (ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_MENUCTL, (void **)&(pMe->m_pIMainMenu)) == SUCCESS)
		{
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_LINE, IDS_DRAW_LINE, NULL, 0);
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_POINT, IDS_DRAW_POINT, NULL, 1);
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_CIRCLE, IDS_DRAW_CIRCLE, NULL, 2);
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_RECTANGLE, IDS_DRAW_RECTANGLE, NULL, 3);
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_TEXT, IDS_DRAW_TEXT, NULL, 4);
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DRAW_PICTURE, IDS_DRAW_PICTURE, NULL, 5);
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_SETTING, IDS_SETTING, NULL, 6);
			result = IMENUCTL_AddItem(pMe->m_pIMainMenu, RESOURCE_RES_FILE, IDS_DOWNLOAD, IDS_DOWNLOAD, NULL, 7);

			IMENUCTL_SetSel(pMe->m_pIMainMenu, (uint16)1);
		}
	}

	if (pMe->m_pIMainMenu)
	{
		IMENUCTL_SetActive(pMe->m_pIMainMenu, TRUE);
		IMENUCTL_Redraw(pMe->m_pIMainMenu);
	}



}



static boolean OnStart(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
	AECHAR szText[] = {'D','r','a','w', '\0'};

	IDISPLAY_DrawText(pMe->m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);

	IDISPLAY_Update (pMe->m_pIDisplay);

	return(TRUE);
}


static boolean OnStop(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
	return TRUE;

}

static boolean OnCommand(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
	MainApp* pMainApp = (MainApp*)pMe;

	if (pMainApp->m_pIMainMenu)
		IMENUCTL_SetActive(pMainApp->m_pIMainMenu, FALSE);

	HideImg(pMainApp);

	IDISPLAY_ClearScreen(pMe->m_pIDisplay);

	//static boolean OnLine(MainApp* pApp);
	//static boolean OnPoint(MainApp* pApp);
	//static boolean Oncircle(MainApp* pApp);
	//static boolean OnRectangle(MainApp* pApp);
	//static boolean OnText(MainApp* pApp);
	//static boolean OnPicture(MainApp* pApp);
	//static boolean OnSetting(MainApp* pApp);


	switch (wParam)
	{
	case IDS_DRAW_LINE:
		OnLine( pMainApp );
		break;
	case IDS_DRAW_POINT:
		OnPoint( pMainApp );

		break;
	case IDS_DRAW_CIRCLE:
		Oncircle( pMainApp );

		break;
	case IDS_DRAW_RECTANGLE:
		OnRectangle( pMainApp );

		break;
	case IDS_DRAW_TEXT:
		OnText( pMainApp );

		break;
	case IDS_DRAW_PICTURE:
		OnPicture( pMainApp );

		break;
	case IDS_SETTING:
		OnSetting( pMainApp );
		break;
	case IDS_DOWNLOAD:
		OnDownload( pMainApp );
		break;
	default:
		return FALSE;
		break;
	}


	return TRUE;
}


static boolean OnKey(AEEApplet * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
{
	MainApp* pMainApp = (MainApp*)pMe;

	switch(wParam)
	{
	case AVK_SOFT1:
		{
			ShowMainMenu(pMainApp);
		}
		return TRUE;
	case AVK_SOFT2:
		{
			int i=0;
		}
		return TRUE;
	case AVK_CLR:
		{
			ShowMainMenu(pMainApp);

		}
		return TRUE;
	default:
		if (pMainApp->m_pIMainMenu&&IMENUCTL_HandleEvent(pMainApp->m_pIMainMenu, eCode, wParam, dwParam))
			return TRUE;
	}



	return FALSE;
}


static void OnLine(MainApp* pApp)
{
	AECHAR szText[] = {'l','i','n','e','\0'};
	IGraphics *pIGra;  
	AEELine line;

	//////////////////////////////////////////////////////////////////////////
	ISHELL_CreateInstance(pApp->a.m_pIShell,AEECLSID_GRAPHICS,(void**)&pIGra);   
	IGRAPHICS_SetBackground(pIGra,0,0,0);    
	IGRAPHICS_ClearViewport(pIGra);  
	IGRAPHICS_SetFillColor(pIGra,255,0,0,0);  
	IGRAPHICS_SetColor(pIGra,0,0,255,0);   


	line.sx = 10;
	line.sy = 20;
	line.ex = 100 ;
	line.ey = 200;
	IGRAPHICS_DrawLine(pIGra, &line);



	IGRAPHICS_Update(pIGra);   
	IGRAPHICS_Release(pIGra);   

	IDISPLAY_DrawText(pApp->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);


}


static void OnPoint(MainApp* pApp)
{
	AECHAR szText[] = {'1','\0'};

	AEEPoint myPt;
	int16 idx = 0 ;

	IGraphics *pIGra;   
	AEEPoint point;   
	AEECircle circle;   
	AEEEllipse ellipise;   



	IDISPLAY_DrawText(pApp->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);



	point.x = 10;   
	point.y = 10;   
	circle.cx = 100;   
	circle.cy = 100;   
	circle.r = 10;   
	ellipise.cx = 50;   
	ellipise.cy = 50;   
	ellipise.wx = 10;   
	ellipise.wy = 20;   


	ISHELL_CreateInstance(pApp->a.m_pIShell,AEECLSID_GRAPHICS,(void**)&pIGra);   
	IGRAPHICS_SetBackground(pIGra,0,0,0);    
	IGRAPHICS_ClearViewport(pIGra);  
	IGRAPHICS_SetFillColor(pIGra,255,0,0,0);  
	IGRAPHICS_SetColor(pIGra,0,0,255,0);   

	//IGRAPHICS_DrawPoint(pIGra,&point); 

	myPt.x = 1u;
	myPt.y = (int16)1;
	for(; idx <100 ; ++idx)
	{
		IGRAPHICS_DrawPoint(pIGra, &myPt);

		myPt.x+=(int16)2;
		myPt.y+=(int16)2;

	}


	IGRAPHICS_Update(pIGra);   
	IGRAPHICS_Release(pIGra);   
}

static void Oncircle(MainApp* pApp)
{
	AECHAR szText[] = {'2','\0'};
	IGraphics *pIGra;  
	AEECircle circle;   
	AEEEllipse ellipise;   




	IDISPLAY_DrawText(pApp->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);

	IDISPLAY_Update (pApp->a.m_pIDisplay);

	//////////////////////////////////////////////////////////////////////////
	circle.cx = 100;   
	circle.cy = 100;   
	circle.r = 10;   
	ellipise.cx = 50;   
	ellipise.cy = 50;   
	ellipise.wx = 10;   
	ellipise.wy = 20;   


	ISHELL_CreateInstance(pApp->a.m_pIShell,AEECLSID_GRAPHICS,(void**)&pIGra);   
	IGRAPHICS_SetBackground(pIGra,0,0,0);    
	IGRAPHICS_ClearViewport(pIGra);  
	IGRAPHICS_SetFillColor(pIGra,255,0,0,0);  
	IGRAPHICS_SetColor(pIGra,0,0,255,0);   

	IGRAPHICS_DrawCircle(pIGra,&circle);   
	//STROKE_DOTTED  


	IGRAPHICS_SetStrokeStyle(pIGra,STROKE_DOTTED);   
	//IGRAPHICS_SetStokeStyle(pIGra,STROKE_SOLID);    
	IGRAPHICS_SetFillColor(pIGra,255,255,0,0);   
	IGRAPHICS_SetColor(pIGra,0,255,255,0);   
	IGRAPHICS_DrawEllipse(pIGra,&ellipise);   


	IGRAPHICS_Update(pIGra);   
	IGRAPHICS_Release(pIGra);   


}

static void OnRectangle(MainApp* pApp)
{
	AECHAR szText[] = {'3','\0'};
	AEERect rect;
	IGraphics *pIGra;  

	IDISPLAY_DrawText(pApp->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);

	IDISPLAY_Update (pApp->a.m_pIDisplay);

	//////////////////////////////////////////////////////////////////////////
	//IGRAPHICS_DrawRect
	rect.x = 30;
	rect.y = 40;
	rect.dx = 40;
	rect.dy  =30;

	ISHELL_CreateInstance(pApp->a.m_pIShell,AEECLSID_GRAPHICS,(void**)&pIGra);   
	IGRAPHICS_SetBackground(pIGra,0,0,0);    
	IGRAPHICS_ClearViewport(pIGra);  
	IGRAPHICS_SetFillColor(pIGra,255,0,0,0);  
	IGRAPHICS_SetColor(pIGra,0,0,255,0);   


	IGRAPHICS_DrawRect(pIGra,&rect);   


	IGRAPHICS_Update(pIGra);   
	IGRAPHICS_Release(pIGra);   

}

static void OnText(MainApp* pApp)
{
	AECHAR szText[] = {'T','E','X','T','\0'};

	IDISPLAY_DrawText(pApp->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);

	IDISPLAY_Update (pApp->a.m_pIDisplay);

}

static void OnPicture(MainApp* pApp)
{
	AECHAR szText[] = {'5','\0'};
	AEEImageInfo ii;

	int SplashSpaceX;
	int SplashSpaceY;

	IDISPLAY_ClearScreen(pApp->a.m_pIDisplay);
	if (!pApp->m_pImage)
		pApp->m_pImage = ISHELL_LoadResImage(pApp->a.m_pIShell, RESOURCE_RES_FILE, IDI_BMP_ICON);

	IIMAGE_GetInfo(pApp->m_pImage, &ii);
	SplashSpaceX = (pApp->m_cxScreen - ii.cx) / 2;
	SplashSpaceY = (pApp->m_cyScreen - ii.cy) / 2;


	IIMAGE_Draw(pApp->m_pImage, SplashSpaceX, SplashSpaceY);

	IDISPLAY_Update(pApp->a.m_pIDisplay);


	IDISPLAY_DrawText(pApp->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);


}

static void OnSetting(MainApp* pApp)
{
	AECHAR szText[] = {'6','\0'};

	IDISPLAY_DrawText(pApp->a.m_pIDisplay,    // Display instance
		AEE_FONT_BOLD,       // Use BOLD font
		szText,              // Text - Normally comes from resource
		-1,                  // -1 = Use full string length
		0,                   // Ignored - IDF_ALIGN_CENTER
		0,                   // Ignored - IDF_ALIGN_MIDDLE
		NULL,                // No clipping
		IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);

	IDISPLAY_Update (pApp->a.m_pIDisplay);

}

static void HideImg(MainApp* pApp)
{
	if(pApp->m_pImage != NULL)
	{
		IIMAGE_Release(pApp->m_pImage);
		pApp->m_pImage = NULL;
	}
	IDISPLAY_ClearScreen(pApp->a.m_pIDisplay);
}


static void OnDownload(MainApp* pApp)
{




}